<?php
class My_API_ss27_Users
{
    private $_config;

    public function __construct($gameId = null)
    {
        if(!isset($gameId) || !file_exists(CONFIG_PATH2 . "/$gameId.ini")){
            return false;
        }
        
        $config = new Zend_Config_Ini(CONFIG_PATH2 . "/$gameId.ini",APPLICATION_ENV);
        $this->_config = $config;
    }
    
    public function getUsersByServerId($listUser, $serverId, $channel = ''){
        $i = 0;
		$infoUsers = array();
		$userCheck = array_chunk($listUser, 50);
		foreach ($userCheck as $listCheck){	
		    $users 		= str_replace("'", "", implode(",", $listCheck));
            if($channel != 'SSGROUP'){ 
                $temp = array();
                foreach($listCheck as $row){
    		      $temp[] = $row.'@'.strtolower($channel);
    		    }
                $users 		= str_replace("'", "", implode(",", $temp));
            }
			
            $urlAPI 	= $this->_config->getUserInfo->url . "?user={$users}&serverId={$serverId}";	
			$result = file_get_contents($urlAPI);   
            $result = json_decode($result);  
			if($result->status == 1){    
                foreach($result->result as $key=>$row){
					$infoUsers[$i]['serverId'] = $serverId;
					$infoUsers[$i]['roleName'] = $row->roleName;

                        if(isset($this->_config->statistics->role))
                        {
                            $title=$this->_config->statistics->role->toArray();
                            foreach($title as $keyTitle => $valTitle)
                            {
                                if(isset($row->$keyTitle))
                                {
                                    $infoUsers[$i][$keyTitle] = $row->$keyTitle;
                                }
                            }
                        }
                    //end
					$i++;
                }
			}else{
				 continue;
			}
		}
      
		return $infoUsers;
    }
    
    public function getUsernameByRolename($listRoleName, $serverId){
        $i = 0;
		$infoUsername = array();
        $serverIdPartner = str_replace('S','',$serverId);
        
		$userCheck = array_chunk($listRoleName, 50);
		foreach ($userCheck as $listCheck){
            $accounts = implode(',',$listCheck);
            $urlAPI = $this->_config->getUsernameInfo->url . "?rolename=$accounts&serverId={$serverIdPartner}";
            $result = file_get_contents($urlAPI);
            $result = json_decode($result);
            
            if(isset($result->status) && $result->status == 1){
                foreach($result->result as $row){
                    
                    foreach($row as $r){
                        if($r->username){
        					$infoUsername[$i]['serverId'] = $serverId;
        					$infoUsername[$i]['username'] = $r->username;
        					//$infoUsername[$i]['roleLevel'] = $r->level;
                              //sua tu cho nay
                            if(isset($this->_config->statistics->role))
                            {
                                $title=$this->_config->statistics->role->toArray();
                                foreach($title as $keyTitle => $valTitle)
                                {
                                    if(isset($r->$keyTitle))
                                    {
                                        $infoUsername[$i][$keyTitle] = $r->$keyTitle;
                                    }
                                }
                            }
                            //end
                        }
                        else
                        {
                            continue;
                        }
    					$i++;
                        
                    }
                }
			}else{
				continue;
			}
		}
       
		return $infoUsername;
    }
}